const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
let user = {};

// 注册
app.post('/zc', (req, res) => {
    user = req.body;
    res.send('注册成功！！')
})

// 登录
app.post('/login', (req, res) => {
    if (req.body.name == user.name && req.body.password == user.password) {
        res.cookie('user', req.body, { maxAge: 10 * 1000 });//cookie过期时间
        res.send('登录成功！！');
    } else {
        res.send('用户名或密码错误')
    }
})

app.get('/test', (req, res) => {

    if (req.cookies.user) {
        if (req.cookies.user.name == user.name && req.cookies.user.password == user.password) {
            res.send('不用手动登录,该返回什么数据就返回什么数据')
        } else {
            res.send('更改过用户名和密码');
        }
    } else {
        res.send('cookie过期');
    }
})

app.post('/gai', (req, res) => {
    user = req.body;
    console.log(user);
    res.send('用户名和密码更改完毕！！')
})

// 端口号
app.listen(3000, err => {
    if (!err)
        console.log('服务器启动成功');
    else
        console.log(err);

}) 